package com.qk.controller;


import com.qk.commen.PageResult;
import com.qk.commen.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping ("/depts")
@RestController
@Slf4j
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 添加
     */
//    @RequestMapping(value = "/depts" ,method = RequestMethod.POST)
    @PostMapping
    public Result add(@RequestBody Dept dept){
        deptService.add(dept);
        log.info("新增部门: {}" , dept);
        return Result.success() ;
    }

    /**
     * 查询
     */
    @GetMapping
    public Result list (String name,
                        Integer status,
                        @RequestParam (defaultValue = "1") Integer page,
                        @RequestParam (defaultValue = "10") Integer pageSize){
        log.info("条件分页查询部门: name={}, status={}, page={}, pageSize={}", name, status, page, pageSize);
        PageResult<Dept> list1  = deptService.list(name, status, page, pageSize);
        return Result.success(list1);
    }

    /**
     * 查询回显示
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
       log.info("查询回显部门: id={}", id);

        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    /**
     * 更新update
     */
    @PutMapping
    public Result update(@RequestBody Dept dept){
      log.info("更新部门: {}", dept);
        deptService.update(dept);
        return Result.success();
    }


    /**
     * 删除delete
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
      log.info("删除部门: {}", id);
        deptService.delete(id);
        return Result.success();
    }
    /**
     * 查询所有部门信息
     */

//    @GetMapping
    @GetMapping("/list")
    public Result list( ){
        List<Dept> depts = deptService.listAll();
        log.info("查询所有部门信息: {}", depts);
        return Result.success(depts );
    }

}
